﻿using Dqs_DAL;
using Model;
using Sunny.UI;

namespace Dqs_UI
{
    public partial class frmQuestionDetail_Teacher : UIForm
    {
        MyDbContext db = new MyDbContext();
        Question currentQuestion;
        public frmQuestionDetail_Teacher(int _qid)
        {
            InitializeComponent();
            currentQuestion = db.Question.Single(t => t.Id == _qid);
        }

        private void frmQuestionDetail_Student_Load(object sender, EventArgs e)
        {
            loadData();
        }

        void loadData()
        {
            dgvQuestionReply.DataSource = null;
            dgvQuestionReply.AutoGenerateColumns = false;
            dgvQuestionReply.DataSource = db.QuestionReply.Where(t => t.QuestionId == currentQuestion.Id).OrderByDescending(t => t.CreateTime).ToList();
            dgvQuestionReply.Refresh();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            string val = string.Empty;
            var result = this.ShowInputStringDialog(ref val, true, "请输入回复", true);
            if (result)
            {
                db.QuestionReply.Add(new Model.QuestionReply
                {
                    QuestionId = currentQuestion.Id,
                    Reply = val,
                    CreateTime = DateTime.Now,
                    TeacherId = frmLogin.LoginTeacherInfo.Id
                });
                currentQuestion.Status = "已回复";
                db.Question.Update(currentQuestion);
                db.SaveChanges();
                loadData();
            }
        }
    }
}
